package ar.edu.unq.wannaplay.pages;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.services.EmployeeService;
import ar.edu.unq.wannaplay.tools.CalendarCreator;
import ar.edu.unq.wannaplay.utils.LicenceCalendar;

public class CalendarPage extends WebPage {
    private static final long serialVersionUID = 1L;

    @SpringBean(name = "service.employeeService")
    private EmployeeService employeeService;

    private LicenceCalendar calendar;

    public CalendarPage() {
    	this.init();
    }

    public void init() {
    	calendar = this.getEmployeeService().getHolidaysCalendar(1, 2011);
        this.addTable();
    }

    private void addTable() {
        this.add(new PageableListView<EmployeeDTO>("calendarTable", this.getEmployeeService().findAllDTOs(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<EmployeeDTO> item) {
                EmployeeDTO employee = item.getModelObject();

                item.add(new Label("employeeName", employee.getName()));
                item.add(new Label("employeeId", employee.getDni()));

                String toShow = "";
                for (int day = 1; day <= CalendarPage.this.getCalendar().getDaysCant(); day++) {
                    if (CalendarPage.this.getCalendar().hasHoliday(employee.getDni(), day)) {
                        toShow = "-----";
                    } else {
                        toShow = "";
                    }
                    item.add(new Label("day" + day, toShow));
                }
            }
        });
        this.addNavegator();
    }

    private void addNavegator() {
        this.add(new Link<WebPage>("nextMonth") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                CalendarPage.this.setCalendar(new CalendarCreator().nextCalendar(CalendarPage.this.getCalendar()));
            }
        });

        this.add(new Link<WebPage>("previousMonth") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                CalendarPage.this.setCalendar(new CalendarCreator().previousCalendar(CalendarPage.this.getCalendar()));
            }
        });
    }

    // setters&getters
    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setCalendar(final LicenceCalendar calendar) {
        this.calendar = calendar;
    }

    public LicenceCalendar getCalendar() {
        return calendar;
    }
}
